using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class HWScriptSignalView : HWScriptCommand 
    {
        SignalView oSignalView;
        SignalFormat eSignalFormat;

        string sFilterValue="";

        public HWScriptSignalView(RapidHardware poRapidHardware, int piClockTime, string psSignalName, SignalFormat peSignalFormat, string psFilterValue)
            : base(poRapidHardware, piClockTime, 95)
        {
            oSignalView = oRapidHardware.SystemInterface.GetSignalView(psSignalName);
            if (oSignalView == null)
                throw new Exception("Signal View Named " + psSignalName + " was not found.");
            eSignalFormat = peSignalFormat;
            sFilterValue = psFilterValue;
        }


        public HWScriptSignalView(RapidHardware poRapidHardware, int piClockTime, string psSignalName, SignalFormat peSignalFormat)
            : base(poRapidHardware,piClockTime,95)
        {
            oSignalView = oRapidHardware.SystemInterface.GetSignalView(psSignalName);
            eSignalFormat = peSignalFormat;
            sFilterValue = "";
        }

        public HWScriptSignalView(RapidHardware poRapidHardware, int piClockTime, string psSignalName)
            : base(poRapidHardware, piClockTime, 95)
        {
            oSignalView = oRapidHardware.SystemInterface.GetSignalView(psSignalName);
            eSignalFormat = oSignalView.SignalFormat;
            sFilterValue = "";
        }

        public override string DoCommand()
        {
            oSignalView.SignalFormat = eSignalFormat;
            string sSignalValue = oSignalView.FormattedValue.Trim();
            if (sFilterValue != "")
            {
                if (sFilterValue.Substring(0, 1) == "!")
                {
                    if (sSignalValue == sFilterValue.Substring(1,sFilterValue.Length - 1))
                        return "";
                }
                else
                {
                    if (sFilterValue.Substring(0, 1) == "=")
                    {
                        if (sSignalValue != sFilterValue.Substring(1, sFilterValue.Length - 1))
                            return "";
                    }
                    else
                    {
                        if (sSignalValue != sFilterValue)
                            return "";
                    }
                }
            }
            return oSignalView.Name + " = {" + sSignalValue + "}";
        }

        public override string ToVerilog()
        {
            return "$display(\"" + ClockTime.ToString() + "," + oSignalView.NameHDL + " = {%b}\"," + oSignalView.NameHDL + ")";
        }
    }
}
